//
//  ProductViewController.m
//  片刻
//
//  Created by lanou3g on 15/6/3.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ProductViewController.h"
#import "RequesetManager.h" //网络请求类
#import "ProductTableViewCell.h" //自定义cell
#import "ProductModel.h" //Model
#import "MaocHeader.h"
@interface ProductViewController ()
@property (nonatomic,retain) UITableView *mainTabelView; //主列表
@property (nonatomic,retain) NSMutableArray *dataSourceArr; //给主列表提供数据的数组
@property(nonatomic, retain) NSMutableArray *arr;
@property (nonatomic,assign,getter=isLoading) BOOL loading;//判断是否正在刷新
@end

@implementation ProductViewController
-(void)dealloc
{
    self.mainTabelView = nil;
    _dataSourceArr = nil;
    _arr = nil;
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.naviLabel.text =@"良品";
    self.view.backgroundColor = [UIColor whiteColor];
    [self ruquestData];
    [self createMainTableView];
}

-(void)createMainTableView
{
    self.mainTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HiGHT-70) style:UITableViewStylePlain];
    _mainTabelView.delegate = self;
    _mainTabelView.dataSource = self;
//    _mainTabelView.rowHeight = 200;
    _loading = NO;
    [_mainTabelView registerClass:[ProductTableViewCell class] forCellReuseIdentifier:@"prductCell"]; //注册
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, 30)];
    label.text = @"下拉刷新.........";
    [self.view addSubview:label];
    [label release];
    _mainTabelView.backgroundColor = [UIColor clearColor];
    
    self.dataSourceArr = [NSMutableArray array];
    
    
    [self.view addSubview:_mainTabelView];
    
    [_mainTabelView release];
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.isLoading) {
        
        if (scrollView.contentOffset.y < -70) {
            _loading = YES;
            [self ruquestData];
            [self createloadingCell];
        }
    }
    

}
//创建下拉时候的一个cell
-(void)createloadingCell{
    [_dataSourceArr insertObject:@"正在刷新" atIndex:0];
//    在tabelView 插入row
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [_mainTabelView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isLoading&&indexPath.row == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.textLabel.text = _dataSourceArr[indexPath.row];
        return cell;
    }else{
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"prductCell" forIndexPath:indexPath];
    ProductModel *model = self.dataSourceArr[indexPath.row];
    cell.model = model;
        return cell;}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isLoading&&indexPath.row == 0) {
        return 70;
    }
    return 200;
}
//请求数据方法
-(void)ruquestData{
    //    网络请求类初始化
    RequesetManager *requ = [[RequesetManager alloc] initWithRequestURL:@"http://api2.pianke.me/pub/shop" requestModel:POST parameyer:nil];
    requ.parameyerDict = [@{@"start":@"0",@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"limit":@"10",@"auth":@"",@"version":@"3.0.2"} mutableCopy];
    requ.delegate = self; //接受协议
    [requ startRequest]; //调用开启请求方法
    
    [requ release];
    
}
#pragma mark - RequestDelegate
//请求完毕后让代理对象执行的方法，同时吧请求完整的data传给代理对象
-(void)requestDidFinshWithData:(NSData *)data
{
    //    JSON解析
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    NSDictionary *dataDic = dic[@"data"];
    NSArray *arr = dataDic[@"list"];
    if (self.isLoading) {
//        删除之前的
        [_dataSourceArr removeAllObjects];
        [_dataSourceArr insertObject:@"正在刷新" atIndex:0];
    }
    for (NSDictionary *dict in arr) {
        ProductModel *model = [[ProductModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        [self.dataSourceArr addObject:model];
        
        [model release];
    }
    [self.mainTabelView reloadData];
    if (self.isLoading) {
//        去掉刷新的cell
        _loading = NO;
        [self deleteCell];
    }
    _loading = NO;
}
//去掉cell方法
-(void)deleteCell
{
    [_dataSourceArr removeObjectAtIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [_mainTabelView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationBottom)];
}
//请求失败后把错误的原因传值给代理对象
-(void)requestError:(NSError *)error
{
    NSLog(@"%@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
